package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Ticket;
import cn.wolfcode.luowowo.article.domain.TicketDetail;
import cn.wolfcode.luowowo.article.domain.TicketTag;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITicketDetailService;
import cn.wolfcode.luowowo.article.service.ITicketService;
import cn.wolfcode.luowowo.article.service.ITicketTagService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/ticket")
public class TicketController {

    @Reference
    private ITicketService ticketService;

    @Reference
    private ITicketDetailService ticketDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private ITicketTagService ticketTagService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @RequestMapping("")
    public String ticketIndex(Model model, Long destId){
        destId = 3261L;
        // 周末活动
        List<Ticket> tickets = ticketService.listByDestId(destId);
        model.addAttribute("tickets", tickets);

        //对应热门城市
        List<Destination> dests = destinationService.selectByHotAndParentId(destId);
        model.addAttribute("dests", dests);

        // 获取对应城市所有景点拥有的前8个标签
        List<TicketTag> tags = ticketTagService.selectTagTOP8(destId);
        model.addAttribute("tags", tags);

        // 获取对应城市和标签的所有景点
        List<Ticket> ts = ticketService.listByDestIdAndTagId(destId, tags.get(0).getId());
        model.addAttribute("ts", ts);
        return "ticket/index";
    }

    @RequestMapping("/ts")
    public String ts(Model model, Long destId){
        // 获取对应城市所有景点拥有的前8个标签
        List<TicketTag> tags = ticketTagService.selectTagTOP8(destId);
        model.addAttribute("tags", tags);
        // 获取对应城市和标签的所有景点
        List<Ticket> ts = ticketService.listByDestIdAndTagId(destId, tags.get(0).getId());
        model.addAttribute("ts", ts);
        return "ticket/indexTpl";
    }

    @RequestMapping("/tsTag")
    public String ts(Model model, Long destId, Long tagId){
        // 获取对应城市和标签的所有景点
        List<Ticket> ts = ticketService.listByDestIdAndTagId(destId, tagId);
        model.addAttribute("ts", ts);
        return "ticket/indexTagTpl";
    }

    @RequestMapping("/detail")
    public String ticketDetail(Model model, Long id){
        Ticket ticket = ticketService.get(id);
        List<TicketDetail> mans = ticketDetailService.listByPrimaryKey(ticket.getId(), TicketDetail.MAN_TICKET);
        List<TicketDetail> childrens = ticketDetailService.listByPrimaryKey(ticket.getId(), TicketDetail.CHILDREN_TICKET);
        ticket.setMinPrice(ticketDetailService.minPrice(id));
        model.addAttribute("ticket", ticket);
        model.addAttribute("mans", mans);
        model.addAttribute("childrens", childrens);
        return "ticket/detail";
    }

    @RequireLogin
    @RequestMapping("/ticketOrder")
    public String ticketOrder(Model model, Long tid){
        TicketDetail ticketDetail = ticketDetailService.get(tid);
        Ticket ticket = ticketService.get(ticketDetail.getTicket().getId());
        model.addAttribute("ticketDetail", ticketDetail);
        model.addAttribute("ticket", ticket);
         return "ticket/addOrder";
    }

    @RequestMapping("/submit")
    @ResponseBody
    public Object submit(String msgCode, String phone, @UserParam UserInfo userInfo){
        String code = userInfoRedisService.getVerifyCode(phone);
        if(!msgCode.equals(code)){
            throw new LogicException("验证码错误!");
        }
       /* order.setUser(userInfo);
        Long orderId = orderTicketService.save(order);*/
        return AjaxResult.success();
    }


   @RequestMapping("/pay")
   @ResponseBody
   public Object pay(String id, Integer status){
//       orderTicketService.updateStatu(id, status);
       return AjaxResult.success();
   }
}
